<?php

namespace app\common\logic;

use app\common\model\trSystemSet;
use app\common\util\ErrorCode;
use app\common\util\Tools;

/**
 * Class Auth
 * @package app\web\service
 * 机场线路逻辑类
 */
class AirportLogic
{
    // 判断路线是否符合设定的 1：日期；2：周末；3：默认
    public static function getJudgeOpenType($data = [], $date = 0){
        if ($data["openDate"] && !empty($data["dateBetween"])) { // 开启了日期
            $data["dateBetween"] = explode(",", $data["dateBetween"]);
            if ($data["dateBetween"][0] <= $date && $date <= $data["dateBetween"][1]) {
                return 1; // 代表 日期
            }
        }elseif ($data["openWeekend"] && (date('w', strtotime($date)) == 6 || date('w', strtotime($date)) == 0)) { // 开启了周末
            return 2; // 代表 周末
        }elseif($data["openDefault"]) { // 开启了默认
            return 3;
        }else{
            return $data["defaultPrice"] ? 3 : ($data["datePrice"] ? 1 : 2);
        }
        return 0;
    }

    // 获取页面展示的线路价格
    public static function getShowPrice($data, $type = 0){ // 1：日期；2：周末；3：默认
        $price = 0;
        switch ($type){
            case "1":
                $price = $data["datePrice"];
                break;
            case "2":
                $price = $data["weekendPrice"];
                break;
            case "3":
                $price = $data["defaultPrice"];
                break;
        }
        return $price;
    }

    // 获取页面展示的成人佣金
    public static function getShowCommission($data, $type = 0){ // 1：日期；2：周末；3：默认
        $price = 0;
        switch ($type){
            case "1":
                $price = $data["dateCommission"];
                break;
            case "2":
                $price = $data["weekendCommission"];
                break;
            case "3":
                $price = $data["defaultCommission"];
                break;
        }
        return $price;
    }

    // 获取备注
    public static function getLineRemark(){
        $lineRemarkData = trSystemSet::getValue("airportRemarks");
        if(empty($lineRemarkData)){
            $lineRemarkData = [
                "airportRemarks" => "",  //机场线路备注
            ];
        }
        return $lineRemarkData;
    }

    // 根据经纬度获取详细地址
    public static function getDetailAddress($latitude, $longitude){
        $key = trSystemSet::getValue("MapSetting");
        if(empty($key)){
            $key = ["TCMapKey" => "FMRBZ-2REKF-5VJJM-NW664-LTAK6-SMBEI"];
        }
        $url = "https://apis.map.qq.com/ws/geocoder/v1/?key={$key["TCMapKey"]}";
        $res = json_decode(Tools::curl($url."&location={$latitude},{$longitude}"), 1);
        if(!isset($res["result"]["address"])){
            return ErrorCode::code("customized", "经纬度获取详细地址失败，请检查腾讯云地图KEY是否正确");
        }
        return $res["result"]["address"];
    }
}